import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class ThreeTeleports
{
	public int shortestDistance(int xMe, int yMe, int xHome, int yHome, String[] teleports)
	{
		long dist = Math.abs(xMe - xHome) + Math.abs(yMe - yHome);
		for(int i=0; i<teleports.length; i++){
			String[] strs = teleports[i].split(" ");
			int x1 = Integer.parseInt(strs[0]);
			int y1 = Integer.parseInt(strs[1]);
			int x2 = Integer.parseInt(strs[2]);
			int y2 = Integer.parseInt(strs[3]);
			String[] teles = new String[teleports.length - 1];
			for(int j=0, k=0; j<teleports.length; j++){
				if(i!=j){
					teles[k++] = teleports[j];
				}
			}
			
			dist = Math.min(dist, Math.abs(xMe - x1) + Math.abs(yMe - y1) + 10L + shortestDistance(x2, y2, xHome, yHome, teles));
			dist = Math.min(dist, Math.abs(xMe - x2) + Math.abs(yMe - y2) + 10L + shortestDistance(x1, y1, xHome, yHome, teles));
		}
		return (int)dist;
	}
	
	public static void main(String[] args)
	{
		long time;
		int answer;
		boolean errors = false;
		int desiredAnswer;
		
		
		time = System.currentTimeMillis();
		answer = new ThreeTeleports().shortestDistance(3, 3, 4, 5, new String[]{"1000 1001 1000 1002", "1000 1003 1000 1004", "1000 1005 1000 1006"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 3;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new ThreeTeleports().shortestDistance(0, 0, 20, 20, new String[]{"1 1 18 20", "1000 1003 1000 1004", "1000 1005 1000 1006"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 14;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new ThreeTeleports().shortestDistance(0, 0, 20, 20, new String[]{"1000 1003 1000 1004", "18 20 1 1", "1000 1005 1000 1006"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 14;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new ThreeTeleports().shortestDistance(10, 10, 10000, 20000, new String[]{"1000 1003 1000 1004", "3 3 10004 20002", "1000 1005 1000 1006"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 30;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new ThreeTeleports().shortestDistance(3, 7, 10000, 30000, new String[]{"3 10 5200 4900", "12212 8699 9999 30011", "12200 8701 5203 4845"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 117;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new ThreeTeleports().shortestDistance(0, 0, 1000000000, 1000000000, new String[]{"0 1 0 999999999", "1 1000000000 999999999 0", "1000000000 1 1000000000 999999999"});
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 36;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		
		
		if (errors)
			System.out.println("Some of the test cases had errors :-(");
		else
			System.out.println("You're a stud (at least on the test data)! :-D ");
	}

}
//Powered by [KawigiEdit] 2.0!
